package kitwee.com.kanpad.bean;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import kitwee.com.kanpad.R;

/**
 * @author GanQuan
 * @date 2018/1/24
 * kitwee.com.kanpad.bean
 */

public class MessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private List<ListDetailBean.KanbanMessageVOListBean> messageList =new ArrayList<>();
    private Context context;
    private LayoutInflater mLayoutInflater;


    public MessageAdapter(Context context, List<ListDetailBean.KanbanMessageVOListBean> messageList) {
        mLayoutInflater= LayoutInflater.from(context);
        this.messageList = messageList;
        this.context = context;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_item, parent, false);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof  MyHolder){
            if(messageList.get(position).getName()!=null){
                ((MyHolder) holder).name.setText("" + messageList.get(position).getName());
            }else{
                ((MyHolder) holder).name.setVisibility(View.GONE);
            }

            ((MyHolder) holder).message.setText("" + messageList.get(position).getSummary());
            long created_time = messageList.get(position).getCreated_time();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd  HH:mm");
            String format = simpleDateFormat.format(created_time);
            ((MyHolder) holder).time.setText("" + format);
            if (mOnItemClickLitener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int pos = holder.getLayoutPosition();
                        mOnItemClickLitener.onItemClick(holder.itemView, pos);
                    }
                });
            }
        }


    }

    @Override
    public int getItemCount() {
        return messageList.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.message)
        TextView message;
        @BindView(R.id.time)
        TextView time;

        public MyHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
}
